/**
 * 
 */
package Verifier.ArithmeticSystem;

import java.util.ArrayList;
import java.util.List;

/**
 * This class represents a product group element which contains a number of elements inside it, which may be one of the following types: GroupElement or ProductGroupElement.
 * This is just like a product field element just for groups.
 * 
 * Note: It implements the product with a list.
 * 
 * @author Moshe Baavur
 *
 */
public class ProductGroupElement extends ProductElement {
	// list to keep all the elements in the element
	List<GroupElement> elements;
	
	/**
	 * Creates a new empty product group element
	 * 
	 * @throws Exception if the group is not of type product group
	 */
	public ProductGroupElement(Group group) throws Exception	{
		super(group);
		// initialize the elements list
		elements = new ArrayList<GroupElement>();
	}
	
	/* (non-Javadoc)
	 * @see Verifier.ArithmeticSystem.ProductElement#addGroupElement(Verifier.ArithmeticSystem.GroupElement)
	 */
	@Override
	public void addGroupElement(GroupElement element) throws Exception {
		// we should check if the element is not exceeding the size of the product and that the element belong
		// to the appropriate group in the product list
		if (elements.size() >= group.getProductSize()) {
			throw new Exception("Adding another element would exceed the size of the product.");
		}
		
		// check that the new element is consistent with the containing group's product
		if (!group.getGroupInProduct(elements.size()).doesElementBelong(element)) {
			throw new Exception("Adding an element which doesn't belong to the product.");
		}
		elements.add(element);
	}
	
	/* (non-Javadoc)
	 * @see Verifier.ArithmeticSystem.ProductElement#getElement()
	 */
	@Override
	public GroupElement getElement(int index) throws IndexOutOfBoundsException {
		return (elements.get(index));
	}
	
	/* (non-Javadoc)
	 * @see Verifier.ArithmeticSystem.ProductElement#getNumberOfElements()
	 */
	@Override
	public int getNumberOfElements() {
		return (elements.size());
	}

	/* (non-Javadoc)
	 * @see Verifier.ArithmeticSystem.ProductElement#createEmpty()
	 */
	@Override
	protected ProductElement createEmpty() {
		try {
			return (new ProductGroupElement(group));
		} catch (Exception e) {
			// we know that we can't get here, cause the object already exists, meaning the group is a product group
			return (null);
		}
	}
}
